package com.cool4j.generator.db

import com.cool4j.generator.config.DBConstants
import com.cool4j.generator.exception.ExceptionMessage
import com.cool4j.generator.exception.GeneratorException
import org.apache.commons.lang3.StringUtils

/**
 * @author Frank create on 16/1/7 19:21
 */
class ConnectionFactory {

    private static DatabaseConnection databaseConnection

    private ConnectionFactory(DatabaseConnection databaseConnection) {
        this.databaseConnection = databaseConnection
    }

    static ConnectionFactory driver(driver) {
        if (StringUtils.equals(DBConstants.DB_DRIVER_ORACLE, driver)) {
            return new ConnectionFactory(new OracleConnection())
        } else if (StringUtils.equals(DBConstants.DB_DRIVER_MYSQL, driver)) {
            return new ConnectionFactory(new MySqlConnection())
        } else {
            throw new GeneratorException(ExceptionMessage.EXCEPTION_JDBC_DRIVER + driver + ExceptionMessage.EXCEPTION_NOT_FOUND)
        }
    }

    DatabaseConnection connection() {
        return this.databaseConnection
    }

}
